package chuangyi.com.org.ViewModel;

import android.content.Context;
import android.databinding.Bindable;
import android.view.View;
import android.widget.Toast;

import com.android.databinding.library.baseAdapters.BR;

import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.SaveContent;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;

/**
 * Created by zxh on 16/12/7.
 */

public class JoinReward extends ObservableWithGoBackHandler {
    private QuestionItem question;
    private String answer;

    @Bindable
    public QuestionItem getQuestion() {
        return question;
    }

    public void setQuestion(QuestionItem question) {
        this.question = question;
    }

    @Bindable
    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
        notifyPropertyChanged(BR.answer);
    }

    public JoinReward(Context context) {
        super(context);
        super.getHandler().setTitle("参与悬赏");
    }

    /**
     * 发表悬赏/提问回复
     *
     * @param view
     */
    public void onClickPublish(View view) {
        if (Utils.isNullOrEmpty(answer)) {
            showToast("回答不能为空");
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (WebApi.partakeanswer(getContext(), question.getId(), answer)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(), "发布成功", Toast.LENGTH_SHORT).show();
                            HomeActivity.popFragment();
                        }
                    });
                }
            }
        }).start();
    }
}
